package com.shwread.android.qysw.step.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shwread.android.qysw.activity.healthy.HealthyChooseFriendActivity;
import com.shwread.android.qysw.step.dto.HMFriend;
import com.shwread.android.qysw.step.view.RectImageView;
import com.shwread.android.qysw.tools.Screen;
import com.shwread.android.qysw10000136.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Fantasy on 16/4/2.
 */
public class ChooseFriendAdapter extends LoadMoreListViewAdapter {

    HealthyChooseFriendActivity mContext;
    public ArrayList<Map<String, Integer>> chooseList;//0是未选择,1是选择

    MyCallBack mMyCallBack;

    public ChooseFriendAdapter(HealthyChooseFriendActivity activity, MyCallBack myCallBack) {
        this.mContext = activity;
        this.chooseList = activity.chooseList;
        this.mMyCallBack = myCallBack;

    }

    @Override
    public RecyclerView.ViewHolder subOnCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.h_friend_choose_item, null);
        rootView.setLayoutParams(new RecyclerView.LayoutParams(Screen.getScreenWidth(), ViewGroup.LayoutParams.WRAP_CONTENT));
        RecyclerView.ViewHolder itemViewHolder = new ChooseViewHolder(rootView);
        return itemViewHolder;
    }

    @Override
    public void subOnBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ChooseViewHolder chooseViewHolder = (ChooseViewHolder) holder;
        final HMFriend hmFriend = (HMFriend) getTotal_data_list().get(position);
        chooseViewHolder.nameText.setText(hmFriend.getNickName());
        chooseViewHolder.dailyStepCountText.setText("日均" + hmFriend.getDailyStep() + "步");
        if (!TextUtils.isEmpty(hmFriend.getUserHead())) {
            Picasso.with(mContext).load(hmFriend.getUserHead()).into(chooseViewHolder.headIconImg);
        }else {
            chooseViewHolder.headIconImg.setImageResource(R.drawable.user_head);
        }
        Map<String, Integer> map = chooseList.get(position);
        if (map.get("isChoose") == 0) {
            chooseViewHolder.chooseCheckbox.setChecked(false);
        } else {
            chooseViewHolder.chooseCheckbox.setChecked(true);
        }
//        chooseViewHolder.chooseCheckbox.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (chooseViewHolder.chooseCheckbox.isChecked()) {
//                    chooseViewHolder.chooseCheckbox.setChecked(false);
//                    chooseList.get(position).put("isChoose", 0);
//                    mMyCallBack.chooseSuccess(chooseList.get(position).get("userId"), 0);
//                } else {
//                    chooseViewHolder.chooseCheckbox.setChecked(true);
//                    chooseList.get(position).put("isChoose", 1);
//                    mMyCallBack.chooseSuccess(chooseList.get(position).get("userId"), 1);
//                }
//            }
//        });
        chooseViewHolder.chooseCheckbox.setClickable(false);
        chooseViewHolder.chooseItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chooseViewHolder.chooseCheckbox.isChecked()) {
                    chooseViewHolder.chooseCheckbox.setChecked(false);
                    chooseList.get(position).put("isChoose", 0);
                    mMyCallBack.chooseSuccess(chooseList.get(position).get("userId"), 0);
                } else {
                    chooseViewHolder.chooseCheckbox.setChecked(true);
                    chooseList.get(position).put("isChoose", 1);
                    mMyCallBack.chooseSuccess(chooseList.get(position).get("userId"), 1);
                }
            }
        });
    }

    static class ChooseViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.choose_item)
        LinearLayout chooseItem;
        @InjectView(R.id.choose_checkbox)
        CheckBox chooseCheckbox;
        @InjectView(R.id.head_icon_img)
        RectImageView headIconImg;
        @InjectView(R.id.name_text)
        TextView nameText;
        @InjectView(R.id.daily_step_count_text)
        TextView dailyStepCountText;

        ChooseViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public interface MyCallBack {
        void chooseSuccess(int userId, int isChoose);
    }
}
